package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class SetIpPhone {

    public static void setIpPhone(DevInfo devInfo) throws UnsupportedEncodingException {

        int dev_id = devInfo.getId();
        String ipxx = "0:0:0:0:0:0:";// 预留的一个空IP监控地址

        String[] ipx =
                (devInfo.getPnServerIp() + ":0:1:" + devInfo.getPnLocalIp() + ":0:1:"
                        + devInfo.getVpnServerIp() + ":0:0:" + devInfo.getVpnLocalIp() + ":0:0:"
                        + ipxx + devInfo.getWiredReportIp() + ":" + devInfo.getWirelessReportIp())
                    .split(":");

        char[] phone = devInfo.getPhoneNum().trim().toCharArray();
        String phoneMsg = devInfo.getSmsContent().trim();

        // String ipxx="0.0.0.0.0.0.";ַ
        // String[]ipx=(iplisterner.getIpa0()+".0.1."+iplisterner.getIpa0()+".0.1."
        // +iplisterner.getIpb0()+".0.0."+iplisterner.getIpb1()+".0.0"
        // +ipxx+iplisterner.getIpPortReport()+iplisterner.getIpPortWireless()).split(".");
        //
        // String ipReport =iplisterner.getIpReport();
        // String ipPortWireless =iplisterner.getIpPortWireless();
        // String ID= iplisterner.getId();

        // char[] phone=iplisterner.getPhone().toCharArray();
        // byte[] message=iplisterner.getMessage().getBytes();

        Socket socket = SocketContent.getInstance().getSocket(dev_id);
        byte[] b3 = new byte[224];// 发送报文指令长度
        byte[] b4 = new byte[10];

        String ID = String.valueOf(dev_id);
        String ipReport = "12009"; // 端口号固定
        String hex = "";
        int ox16; // 以16进制方式存储到byte中
        String[] s0 =
                { "61", "31", "30", "2c", "88", "87", "04", "06", "d6", "00", "00", "00", "00",
                 "00", "00", "00" };

        // String
        // []ipx={"172","21","6","28","0","1","172","21","6","28","0","1",
        // "172","21","6","233","0","0","172","21","6","28","0","0",
        // "0","0","0","0","0","0","172","21","6","28","172","21","6","28"};
        //
        // char[] phone={'1','3','3','1','9','4','0','3','2','0','8'};
        // String phoneMsg="I love You , XiMei";

        int len = phoneMsg.length();
        for (int i = 0; i < 12; i++) {
            hex = s0[i];
            if (i == 10)
                hex = ID;
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b4 = HexStringToBytes.hexStringToBytes(hex);
            b3[i] = b4[0];
        }
        hex = ipReport;
        int port = Integer.parseInt(hex); // 有线端口号设置
        hex = Integer.toHexString(port);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        b4 = HexStringToBytes.hexStringToBytes(hex);
        b3[12] = b4[1];
        b3[13] = b4[0];

        hex = ipReport;
        port = Integer.parseInt(hex); // 无线端口号设置
        hex = Integer.toHexString(port);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        b4 = HexStringToBytes.hexStringToBytes(hex);
        b3[14] = b4[1];
        b3[15] = b4[0];

        // 监听四个IP地址的个数设置
        for (int i = 16; i < 20; i++) {
            if (i == 16)
                hex = "4";
            else
                hex = "0";
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b4 = HexStringToBytes.hexStringToBytes(hex);
            b3[i] = b4[0];
        }
        // 四个监听IP地址，一个预留IP地址，以及两个上报IP设置
        for (int i = 20; i < 58; i++) {
            ox16 = Integer.parseInt(ipx[i - 20]);
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b4 = HexStringToBytes.hexStringToBytes(hex);
            b3[i] = b4[0];

        }

        for (int i = 58; i < 58 + phone.length; i++) {

            ox16 = (int) (phone[i - 58]);
            hex = Integer.toHexString(ox16);
            hex = (hex.length() == 2 ? hex : ("0" + hex));
            b4 = HexStringToBytes.hexStringToBytes(hex);
            b3[i] = b4[0];

        }

        // Msg内容长度设置

        hex = Integer.toHexString(len * 2);// len*2转变为16进制的字符串
        hex = (hex.length() == 2 ? hex : ("0" + hex));

        b4 = HexStringToBytes.hexStringToBytes(hex);
        b3[80] = b4[0];

        // Msg 短信内容设置
        char[] message = phoneMsg.toCharArray();

        for (int i = 84, j = 0; i < 84 + message.length; i++) {

            byte[] bts = String.valueOf(message[i - 84]).getBytes("GBK");
            byte[] bx = new byte[2];
            bx = bts.length == 1 ? new byte[] { bts[0], 0x00 } : bts;
            b3[i + j] = bx[0];
            b3[i + j + 1] = bx[1];
            j++;
        }

        // for (int j = 0; j < 224; j++) {
        // System.out.print(b3[j]+" ");
        // }

        try {
            OutputStream out = socket.getOutputStream();
            out.write(b3);
            out.close();

        } catch (IOException e) {
            System.out.println("设置失败");
        }

    }

}
